package com.foryou.wechat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.foryou.wechat.entity.InviteInfoEntity;
import com.foryou.wechat.entity.SysMsgEntity;
import com.foryou.wechat.mapper.InviteInfoMapper;
import com.foryou.wechat.mapper.SysMsgMapper;
import com.foryou.wechat.model.vo.SysMsgItemVo;
import com.foryou.wechat.model.vo.SysMsgVo;
import com.foryou.wechat.service.SysMsgService;
import com.foryou.wechat.utils.RelativeDateFormat;
import com.foryou.wechat.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h1>系统消息接口汇总</h1>
 * Created by Lee.
 */

@Service
public class SysMsgServiceImpl implements SysMsgService {

    @Autowired
    private InviteInfoMapper inviteInfoMapper;

    @Autowired
    private SysMsgMapper sysMsgMapper;

    @Override
    public Response getSysMsgIndex(Long userId) {
        Map<String, Object> result = new HashMap<>();
        //1.查询收到的约拍请求
            //1.1查询未读的约拍请求数量
        int countReceived = inviteInfoMapper.selectCount(new QueryWrapper<InviteInfoEntity>()
                .eq("to_user_id", userId)
                .eq("un_read",0));
        //1.2查询最后一条未读的约拍请求
        InviteInfoEntity toUserLastedInfo = inviteInfoMapper.getToUserLastedInfo(userId);
        SysMsgItemVo item1 = null;
        if (toUserLastedInfo != null) {
            item1 = new SysMsgItemVo(toUserLastedInfo.getReason(),
                    RelativeDateFormat.format(toUserLastedInfo.getCreateTime()), countReceived);
        }
        result.put("receivedInvite", item1);

        SysMsgItemVo item2 = null;
        //2.查询发起的约拍请求
            //2.1查询未读的约拍请求数量
        int countSend = inviteInfoMapper.selectCount(new QueryWrapper<InviteInfoEntity>()
                .eq("from_user_id", userId)
                .eq("un_read",0));
            //2.2查询最后一条未读的约拍请求
        InviteInfoEntity toFromLastedInfo = inviteInfoMapper.getFromUserLastedInfo(userId);
        if (toFromLastedInfo != null) {
            item2 = new SysMsgItemVo(toFromLastedInfo.getReason(),
                    RelativeDateFormat.format(toFromLastedInfo.getCreateTime()), countSend);
        }
        result.put("sendInvite", item2);

        SysMsgItemVo item3 = null;
        //3.查询系统消息
            //3.1查询未读的系统消息数量
        int countUnRead = sysMsgMapper.selectCount(new QueryWrapper<SysMsgEntity>()
                .eq("user_id", userId)
                .or()
                .isNull("user_id")
        );
        //3.2查询最后一条未读的系统消息
        SysMsgEntity lastedSysMsgInfo = sysMsgMapper.getLastedSysMsgInfo(userId);
        if (lastedSysMsgInfo != null) {
            item3 = new SysMsgItemVo(lastedSysMsgInfo.getContent(),
                    RelativeDateFormat.format(lastedSysMsgInfo.getCreateTime()), countUnRead);
        }
        result.put("sysMsg", item3);
        return Response.success(result);
    }

    @Override
    public Response getSysMsgList(int start,Long userId) {
        List<SysMsgEntity> entityList = sysMsgMapper.querySysMsgList(start, userId);
        List<SysMsgVo> msgVoList = new ArrayList<>();
        entityList.forEach(e->{
            msgVoList.add(new SysMsgVo(
                    RelativeDateFormat.format(e.getCreateTime()),
                    e.getTitle(),
                    e.getContent()
            ));
        });
        return Response.success(msgVoList);
    }
}
